package com.example.su_shi_tan_sig_in

import android.os.Bundle
import android.view.View
import androidx.fragment.app.FragmentStatePagerAdapter
import com.alibaba.android.arouter.facade.annotation.Route
import com.example.su_shi_tan_bas_lib.base.BaseDataBindingFragment
import com.example.su_shi_tan_bas_lib.base.BaseFragmentEx
import com.example.su_shi_tan_bas_lib.global.AppGlobals
import com.example.su_shi_tan_bas_lib.utils.even.MessageWrap
import com.example.su_shi_tan_bas_lib.utils.even.USER_LOGInED
import com.example.su_shi_tan_core.arouter.SignInARouter
import com.example.su_shi_tan_core.getUserInfo
import com.example.su_shi_tan_core.reStartMainActivity
import com.example.su_shi_tan_sig_in.child.LoginAccountFragment
import com.example.su_shi_tan_sig_in.child.LoginProxy
import com.example.su_shi_tan_sig_in.child.LoginProxyImp
import com.example.su_shi_tan_sig_in.child.RegisterFragment
import com.example.su_shi_tan_sig_in.databinding.SignInFragmentBinding
import me.yokeyword.fragmentation.anim.DefaultVerticalAnimator
import me.yokeyword.fragmentation.anim.FragmentAnimator
import org.greenrobot.eventbus.EventBus

@Route(path = SignInARouter.SignInFragment)
class SignInFragment : BaseDataBindingFragment<SignInFragmentBinding>() {

    override fun layoutId(): Int {
        return R.layout.sign_in_fragment
    }

    private val pageArray = arrayOf<BaseFragmentEx>(
        LoginAccountFragment.newInstance(
            object:LoginProxyImp(R.string.Login){
                override fun intoSignUp(){
                    viewBinder.mViewPager.currentItem = 1
                }
                override fun onLogin(){
//                    getUserInfo(_mActivity)
//                    EventBus.getDefault().post(MessageWrap.getInstance(USER_LOGInED))
//                    pop()
                    reStartMainActivity()
                }
            }
        ),
        RegisterFragment.newInstance(
            object : LoginProxyImp(R.string.register) {
                override fun intoSignIn() {
                    viewBinder.mViewPager.currentItem = 0
                }
            }
        )
    )

    private var mPagerAdapter: FragmentStatePagerAdapter? = null

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        mPagerAdapter = object
            :
            FragmentStatePagerAdapter(childFragmentManager, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {

            override fun getCount() = pageArray.size

            override fun getItem(position: Int) = pageArray[position]

            override fun getPageTitle(position: Int): CharSequence? {
                val walletLabInfo = getItem(position) as LoginProxy
                return AppGlobals.getApplication().getString(walletLabInfo.title)
            }

        }

        viewBinder.mViewPager.adapter = mPagerAdapter

        viewBinder.mTabLayout.setupWithViewPager(viewBinder.mViewPager)
    }


    override fun onCreateFragmentAnimator(): FragmentAnimator {
        return DefaultVerticalAnimator()
    }

}